//
//package com.itheima.service;
//
//import com.itheima.pojo.Role;
//import com.itheima.pojo.User;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.security.core.GrantedAuthority;
//import org.springframework.security.core.authority.AuthorityUtils;
//import org.springframework.security.core.userdetails.UserDetails;
//import org.springframework.security.core.userdetails.UserDetailsService;
//import org.springframework.security.core.userdetails.UsernameNotFoundException;
//import org.springframework.stereotype.Service;
//
//import java.util.List;
//import java.util.Set;
//import java.util.stream.Collectors;
//
//@Service
//public class SpringSecurityUserService implements UserDetailsService {
//    //查找服务，实现查询数据库
//    @Autowired
//    private UserService userService;
//
//    //根据用户名查询数据库中用户信息
//    @Override
//    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
//
//        //根据用户名查询用户信息
//        User user = userService.findByUsername(username);
//
//        if(user==null){
//            //如果没有查询到用户信息，抛出异常
//            throw new UsernameNotFoundException("用户不存在");
//        }
//
//        Set<Role> roles = user.getRoles() ;
//        // 把角色转换成数组
//        String[] roleCodes = roles.stream()
//                .map(Role::getKeyword)
//                .collect(Collectors.toList())
//                .toArray(new String[0]);
//        List<GrantedAuthority> authorityList = AuthorityUtils.createAuthorityList(roleCodes);
//        user.setAuthorities(authorityList);
//
//        UserDetails userDetails = new org.springframework.security.core.userdetails.User(user.getUsername(), user.getPassword(), authorityList);
//
//        return userDetails;
//    }




//}

